package com.twu.biblioteca.App;

import com.twu.biblioteca.Models.*;

import java.util.Scanner;

public class BibliotecaApp {

    private String displayMessage;
    private BooksManager booksManager;
    private MovieManager movieManager;
    private SessionManager sessionManager;
    private UserManager userManager;
    private Scanner scanner;


    public BibliotecaApp() {
        booksManager = new BooksManager();
        movieManager = new MovieManager();
        sessionManager = new SessionManager();
        userManager = new UserManager();
        scanner = new Scanner(System.in);

    }

    public static void main(String[] args) {
        BibliotecaApp app = new BibliotecaApp();
        app.StartApplication();
    }

    public void StartApplication() {
        displayMessage = getWelcomeMessage() + getMenu();
        System.out.println(getDisplayMessage());
        do {
            System.out.println(getDisplayMessage());
            int choice = getUserChoice();
            performAction(choice);
            System.out.println(getDisplayMessage());
            displayMessage = getMenu();
        } while (true);
    }

    public String getWelcomeMessage() {
        return "Welcome to the Biblioteca Library Management System \n";
    }

    public String getDisplayMessage() {
        return displayMessage;
    }

    public String getMenu() {
        String menuOptions = "\n      Menu \nPlease choose one of the following options :\n" +
                "1. Browse Books\n" +
                "2. Reserve a Book\n" +
                "3. Membership Details\n" +
                "4. View Movies\n" +
                "5. " + loginButton() + "\n" +
                "6. Exit\n";
        return menuOptions;
    }

    private String loginButton() {
        return sessionManager.isCurrentSessionLoggedIn() ? "Logout" : "Login";
    }

    public int getUserChoice() {
        System.out.println("Enter Choice : ");
        String nextIntString = scanner.nextLine();  //get the number as a single line
        int choice = Integer.parseInt(nextIntString);    //convert the string to an int
        return choice;
    }

    public void performAction(int choice) {
        switch (choice) {
            case 1:
                displayMessage = booksManager.showBooks();
                break;

            case 2:
                if (sessionManager.isCurrentSessionLoggedIn()){
                    System.out.println("Enter Book Id to reserve : ");
                    String nextIntString = scanner.nextLine();
                    int bookID = Integer.parseInt(nextIntString);
                    if(booksManager.reserveBook(bookID))
                        displayMessage = "Thank You! Enjoy the book.\n";
                    else
                        displayMessage = "Sorry we don't have that book yet. \n";
                }
                else
                    displayMessage = "Please Log in first\n";
                break;

            case 3:
                displayMessage = getMembershipDetails();
                break;

            case 4:
                displayMessage = movieManager.showMovies();
                break;

            case 5:
                if (sessionManager.isCurrentSessionLoggedIn())
                    logout();
                else
                    login();
                break;

            case 6:
                System.out.println("Thank you for using our system");
                System.exit(0);
                break;

            default:
                displayMessage = "Select a valid option!! \n";
                break;
        }
    }

    private void login() {
        if (sessionManager.isCurrentSessionLoggedIn())
            displayMessage = "User is already logged in.";
        else {
            System.out.println("\nEnter username : \n");
            String nextIntString = scanner.nextLine();      //get the number as a single line
            int username = Integer.parseInt(nextIntString); //convert the string to an int
            System.out.println("\nEnter password : \n");
            String password = scanner.nextLine();
            password = password.trim();
            // @TODO see format of Password
            if(userManager.validateLogin(username, password)) {
                sessionManager.setCurrentSessionId(username);
                displayMessage = "Login Successful";
            }
            else {
                displayMessage = "Failed to Log in. Please try again.";
            }

        }
    }

    public void logout() {
        sessionManager.endCurrentSession();
        displayMessage = "Logout Successful";
    }

    public String getMembershipDetails() {
        String message = "";
        if (sessionManager.isCurrentSessionLoggedIn())
            message = userManager.getUserMembershipDetails(sessionManager.getCurrentSessionId());
        else
            message = "\nPlease talk to a Librarian. Thank you.\n";
        return message;
    }
}